package dice.sinanya.db.des;

import dice.sinanya.db.tools.DbUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nlpcn.commons.lang.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static dice.sinanya.db.rules.Rule.getUniqueList;
import static dice.sinanya.db.rules.Rule.makeArray;
import static dice.sinanya.tools.getNLP.getNlp;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;


/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: 查询KP主群类，刷写到静态变量中，只在静态变量中找不到时才需要使用
 */
public class SelectDes {
    private static final Logger log = LogManager.getLogger(SelectDes.class.getName());


    public SelectDes() {
        //        初始化时无需逻辑
    }

    /**
     * 刷新kp主群设定到静态变量中，只有静态变量中找不到某人的kp主群记录时才会使用
     */
    public String selectDes(long qqId, String key) {
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select `value` from test.des where qqId=? and `key`=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setLong(1, qqId);
                ps.setString(2, key);
                try (ResultSet set = ps.executeQuery()) {
                    if (set.next()) {
                        return set.getString("value");
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        ArrayList<String> keyList = makeArray(getNlp(key));
        ArrayList<String> result = new ArrayList<>();
        try (Connection conn = DbUtil.getConnection()) {
            for (String singleKey : keyList) {
                String sql = stringFormatById("select `key` from test.des where `key` like '%%%s%%' and qqId=?", singleKey);
                try (PreparedStatement ps = conn.prepareStatement(sql)) {
                    ps.setLong(1, qqId);
                    try (ResultSet set = ps.executeQuery()) {
                        while (set.next()) {
                            result.add(set.getString("key"));
                        }
                    }
                }
            }
            result = getUniqueList(result);
            return "不存在此条目:\t" + key + "\n猜测是以下条目:\n" + StringUtil.joiner(result, "\n");
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        System.gc();
        return "未找到";
    }

    /**
     * 刷新kp主群设定到静态变量中，只有静态变量中找不到某人的kp主群记录时才会使用
     */
    public ArrayList<String> selectDesList(long qqId) {
        ArrayList<String> result = new ArrayList<>();
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select `key` from test.des where qqId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setLong(1, qqId);
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        result.add(set.getString("key"));
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }
}
